package com.glodon.paas.databag.resource;

import javax.ws.rs.DELETE;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.glodon.paas.databag.service.DatabagService;

/**
 * databag公开权限控制
 * 
 * @author botao.liao
 * 
 */
@Component
@Path("/internal/public")
public class PublicResource {
    private Logger logger = LoggerFactory.getLogger(PublicResource.class);

    @Autowired
    private DatabagService databagService;

    /**
     * 公开一个文档.
     * 
     * @param databagId
     * @return 公开的token
     */
    @PUT
    @Path("/{databagId}")
    public Response publish(@PathParam("databagId") String databagId) {
        logger.info("public databag '{}'", databagId);
        String token = databagService.publicDatabag(databagId);
        Response resp = Response.ok(token, MediaType.APPLICATION_JSON_TYPE).build();
        return resp;
    }

    @DELETE
    @Path("/{databagId}")
    public void revoke(@PathParam("databagId") String databagId) {
        logger.info("remoking public databag '{}'", databagId);
        databagService.revokeDatabag(databagId);
    }
}
